package com.jk.service;

import com.jk.entity.BankCard;
import com.jk.entity.Payment;
import com.jk.entity.TransferRecord;
import com.jk.entity.User;
import com.jk.mapper.PaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service("paymentService")
public class PaymentServiceImpl implements PaymentService{
    @Autowired
    private PaymentMapper paymentMapper;

    /**
     * 查询余额数据
     * @param userId
     * @return
     */
    public Payment queryPayment(String userId) {
        return paymentMapper.queryPayment(userId);
    }
    /**
     * 查询银行卡余额
     * @param userId
     * @return
     */
    public BankCard queryBank(String userId) {
        return paymentMapper.queryBank(userId);
    }
    /**
     * 新增数据
     * @param payment
     */
    public void addPayment(Payment payment){
        paymentMapper.addPayment(payment);
    }
    /**
     * 删除数据
     * @param accountId
     * @return
     */
    public void deletePayment(String accountId) {
        paymentMapper.deletePayment(accountId);
    }
    /**
     * 充值（将账户余额转入余额宝余额）
     * @param userId
     * @param paymentInto
     * @return
     */
    public boolean updatePayment(String userId, double paymentInto) {
        Payment payment = paymentMapper.queryPayment(userId);
        if(paymentInto<payment.getAccountBalance()){
            paymentMapper.updateYuebao(userId,paymentInto);
            paymentMapper.updateZhyue(userId,paymentInto);

            TransferRecord tr = new TransferRecord();
            tr.setId(UUID.randomUUID().toString().replace("-",""));
            tr.setRecordName("充值余额宝-单次充值");
            tr.setDealMoney(paymentInto);
            tr.setDealStatus("交易成功");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String ss = sdf.format(new Date());
            tr.setDealDate(ss);
            paymentMapper.addPaymentRecord(tr);
            return true;
        }else{
            return false;
        }
    }
    /**
     * 充值（将银行卡余额充值至账户余额）
     * @param userId
     * @param paymentInto
     * @return
     */
    public boolean updateBankYue(String userId, double paymentInto) {
        BankCard bank = paymentMapper.queryBank(userId);
        if(paymentInto<bank.getBankBalance()){
            paymentMapper.updateBankYue(userId,paymentInto);
            paymentMapper.updateBankZhyue(userId,paymentInto);

            TransferRecord tr = new TransferRecord();
            tr.setId(UUID.randomUUID().toString().replace("-",""));
            tr.setRecordName("充值余额-单次充值");
            tr.setDealMoney(paymentInto);
            tr.setDealStatus("交易成功");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String ss = sdf.format(new Date());
            tr.setDealDate(ss);
            paymentMapper.addPaymentRecord(tr);
            return true;
        }else{
            return false;
        }
    }
    /**查询银行卡卡号*/
    public BankCard queryBankId(String userId) {
        return paymentMapper.queryBankId(userId);
    }
    /**查询手机号码*/
   /* public User queryPhoneNumber(String userId) {
        return paymentMapper.queryPhoneNumber(userId);
    }*/

    /**
     * 提现（将余额提现至银行卡）
     * @param userId
     * @param paymentInto
     * @return
     */
    public boolean updateTiXian(String userId, double paymentInto) {
        Payment payment= paymentMapper.queryPayment(userId);
        if(payment.getAccountBalance()>paymentInto){
            paymentMapper.updateBankOut(userId,paymentInto);
            paymentMapper.updateBankRoll(userId,paymentInto);

            TransferRecord tr = new TransferRecord();
            tr.setId(UUID.randomUUID().toString().replace("-",""));
            tr.setRecordName("余额-单次提现");
            tr.setDealMoney(paymentInto);
            tr.setDealStatus("交易成功");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String ss = sdf.format(new Date());
            tr.setDealDate(ss);
            paymentMapper.addPaymentRecord(tr);
            return true;
        }else{
            return false;
        }
    }

}
